package com.xiaoyang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoyang.common.BaseContext;
import com.xiaoyang.common.R;
import com.xiaoyang.domain.OrderDetail;
import com.xiaoyang.domain.Orders;
import com.xiaoyang.dto.OrdersDto;
import com.xiaoyang.service.OrderDetailService;
import com.xiaoyang.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @auther XiaoYang
 * @DATE 2022/12/18-17:40
 */
@RestController
@Slf4j
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    @GetMapping("/page")
    public R<Page<Orders>> page(int page, int pageSize, String number, String beginTime,String endTime){

        //构造分页构造器
        Page<Orders> pageInfo = new Page<>(page,pageSize);

        //构造条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(number),Orders::getNumber,number);
        queryWrapper.between(StringUtils.isNotEmpty(beginTime),Orders::getCheckoutTime,beginTime,endTime);
        //添加排序条件
        queryWrapper.orderByDesc(Orders::getCheckoutTime);
        //执行查询
        ordersService.page(pageInfo,queryWrapper);

        BaseContext.close();

        return R.success(pageInfo);
    }

    @PutMapping
    public R<String> updateStatus(@RequestBody Orders orders){
        ordersService.updateById(orders);
        BaseContext.close();
        return R.success("修改成功");
    }

    @GetMapping("/userPage")
    public R<Page<OrdersDto>> userPage(int page, int pageSize){
        //构造分页构造器
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        Page<OrdersDto> dtoPage = new Page<>();

        //构造条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件
        queryWrapper.orderByDesc(Orders::getCheckoutTime);
        queryWrapper.eq(Orders::getUserId,BaseContext.getCurrentId());
        //执行查询
        ordersService.page(pageInfo,queryWrapper);
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");

        List<Orders> records = pageInfo.getRecords();
        List<OrdersDto> list = new ArrayList<>();
        records.stream().map((item) -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item,ordersDto);

            Long id = item.getId();

            //根据id查询订单详情
            LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
            orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId,id);
            List<OrderDetail> details = orderDetailService.list(orderDetailLambdaQueryWrapper);
            if (details != null){
                ordersDto.setOrderDetails(details);
                list.add(ordersDto);
            }
            return ordersDto;
        }).collect(Collectors.toList());

        dtoPage.setRecords(list);
        log.info("{}",list);

        BaseContext.close();

        return R.success(dtoPage);
    }

    @PostMapping("/again")
    public R<String> again(){
        return R.success("先别来了");
    }

}
